package com.javaforever.clocksimplejee4.facade;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.Map;
import java.util.TreeMap;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.baidu.ai.aip.utils.Base64Util;
import com.javaforever.clocksimplejee4.serviceimpl.FaceServiceImpl;
import com.javaforever.clocksimplejee4.utils.StringUtil;

import net.sf.json.JSONObject;

public class DetectOneValidFaceFacade extends HttpServlet {
	private static final long serialVersionUID = -5622204132605795420L;

	protected void processRequest(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
	     response.setContentType("text/html;charset=UTF-8");
	     PrintWriter out = response.getWriter();
	     Map<String, Object> result = new TreeMap<String,Object>();
	     try {
	    	 String imageData = request.getParameter("image");
	         FaceServiceImpl fimpl = new FaceServiceImpl();
	         boolean success = false;
	         if (!StringUtil.isBlank(imageData)) success = fimpl.detectOneValidFace(Base64Util.decode(imageData));
             result.put("success", success);
             result.put("data", null);
             out.println(JSONObject.fromObject(result));
	     } catch (Exception e){
	    	 e.printStackTrace();
             result.put("success", false);
             result.put("errorMessage", e.getMessage());
             out.println(JSONObject.fromObject(result));
          } finally {
	         out.close();
	     }
 }

 @Override
 protected void doGet(HttpServletRequest request, HttpServletResponse response)
 throws ServletException, IOException {
     processRequest(request, response);
 }

 @Override
 protected void doPost(HttpServletRequest request, HttpServletResponse response)
 throws ServletException, IOException {
     processRequest(request, response);
 }

 @Override
 public String getServletInfo() {
     return "Short description";
 }// </editor-fold>

}
